package com.whx.managea.controller;

import com.whx.managea.entity.Student;
import com.whx.managea.service.StudentService;
import com.whx.managea.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    Student student;

    //定义一个请求用来查询所有的学生信息
    @RequestMapping("findAll")
    public String findAll(Integer page,String searchCol,String searchValue,Model model){
       //1.获取用户查询的当前页
        page=page==null?1:page;
        //2.获取每页显示的条数
        Integer rows=2;

        //获取用户输入的条件
        System.out.println(searchCol+"----"+searchValue);
        //调用业务层
        //3.求每页显示的学生列表
        List<Student> students = studentService.findAll(page,rows,searchCol,searchValue);
        //4.总记录数
        Integer counts = studentService.counts(searchCol, searchValue);
        //5.总页数
        Integer pages=counts%rows==0?counts/rows:counts/rows+1;
        //2.设置在域中
        model.addAttribute("students",students);
        model.addAttribute("page", page);
        model.addAttribute("pages", pages);
        model.addAttribute("counts", counts);
        model.addAttribute("searchCol",searchCol);
        model.addAttribute("searchValue",searchValue);
        //3.转发到前端页面
        return "back/student/index";
    }
    //学生添加方法
    @RequestMapping("save")
    public String save(HttpServletRequest request,String[] tagIds){
        String name=request.getParameter("name");
        String bir=request.getParameter("bir");
        String phone=request.getParameter("phone");
        String qq=request.getParameter("qq");
        String mark=request.getParameter("mark");
        String cityid=request.getParameter("cityid");
        String clazzid=request.getParameter("clazzid");
        String groupid=request.getParameter("groupid");
        student.setBir(DateUtil.convertDateStr(bir)).setCityid(cityid).setClazzid(clazzid).setGroupid(groupid)
        .setQq(qq).setPhone(phone).setMark(mark).setName(name);
        System.out.println(tagIds);
        studentService.save(student,tagIds);
        return "redirect:/student/findAll";
    }


}
